from presenter import basepresenter
from domain import topic

class PostTopicPresenter(basepresenter.BasePresenter):

    def respondToGet(self):
        # check for valid user profile first
        if (self.currentuserprofile):
            # determine if post is for a discussion
            if (self.request.get('discussion')):
                thediscussion = self.discussiontask.getDiscussionByKey(self.request.get('discussion'))
                self.template_values['discussion'] = thediscussion
            if (self.request.get('article')):
                thearticle = self.articletask.getByKey(self.request.get('article'))
                self.template_values['article'] = thearticle
            if (self.request.get('document')):
                thedocument = self.documenttask.getByKey(self.request.get('document'))
                self.template_values['document'] = thedocument
            # set up the page title
            page_title = self.sitetitle + ' - New Post'
            self.template_values['page_title'] = page_title

            #get the view to draw the page
            self.view.showGetResponse(self.template_values)
        else:
            self.view.redirectToUrl('/')

    def respondToPost(self):
        # check for valid user profile first
        if (self.currentuserprofile):
             # populate a topic object from the request
            newuser = self.currentuser
            newnickname = self.currentuserprofile.nickname
            newtitle = self.request.get('title')
            newmessage = self.request.get('message')
            if self.request.get('discussionkey'):
                newtype = 'Discussion'
                thediscussion = self.discussiontask.getDiscussionByKey(self.request.get('discussionkey'))
                querystring='/discussiontopics?discussion=' + str(thediscussion.key())
                newtopic = topic.Topic(title=newtitle, message=newmessage, user=newuser, nickname=newnickname, type=newtype, discussion=thediscussion)
                newtopic.enabled = True
            if self.request.get('articlekey'):
                newtype = 'Article'
                thearticle = self.articletask.getByKey(self.request.get('articlekey'))
                querystring='/articletopics?article=' + str(thearticle.key())
                newtopic = topic.Topic(title=newtitle, message=newmessage, user=newuser, nickname=newnickname, type=newtype, article=thearticle)
                newtopic.enabled = True
            if self.request.get('documentkey'):
                newtype = 'Document'
                thedocument = self.documenttask.getByKey(self.request.get('documentkey'))
                querystring='/documenttopics?document=' + str(thedocument.key())
                newtopic = topic.Topic(title=newtitle, message=newmessage, user=newuser, nickname=newnickname, type=newtype, document=thedocument)
                newtopic.enabled = True
            # create the task and save the article
            self.discussiontask.saveTopic(newtopic, self.currentuserprofile)
            #get the view to draw the page
            self.view.showPostResponse(self.template_values, querystring)
        else:
           self.view.redirectToUrl('/')